package com.bjpowernode.web.controller;

import com.bjpowernode.api.dto.ProductBidInfo;
import com.bjpowernode.api.dto.ProductCollection;
import com.bjpowernode.api.dto.ProductIncomeInfo;
import com.bjpowernode.api.dto.UserBidInfo;
import com.bjpowernode.api.model.FinanceAccount;
import com.bjpowernode.api.model.Product;
import com.bjpowernode.util.CommonUtils;
import com.bjpowernode.web.resp.ResponseObj;
import com.bjpowernode.web.resp.view.ProductBidView;
import com.bjpowernode.web.resp.view.ProductIncomeView;
import com.bjpowernode.web.resp.vo.PageInfo;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author 麦翔盛
 * @ClassName: ProductService
 * @Package: com.bjpowernode.web.controller
 * @description: 产品 控制层
 * @date 2022/5/14 22:25
 */
@Api(tags = "投资产品接口")
@RestController
public class ProductController extends BaseController {

    /**
     * 首页展示产品的基本信息
     *
     * @return 三种产品的基本信息
     */
    @GetMapping("v1/product/index")
    @ApiOperation(value = "获取投资产品的基本信息", notes = "获取新手宝(1)，优选产品(3)，散户产品(3)列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功", response = ResponseObj.class),
            @ApiResponse(code = 500, message = "服务器内部异常", response = String.class)
    })
    @ApiImplicitParams({

    })
    public ResponseObj mainProductList() {
        ProductCollection productCollection = productService.queryMainProductList();

        return ResponseObj.ok().content(productCollection);
    }


    /**
     * 分页带条件查询产品列表
     * @param productType
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "分页带条件查询产品列表", notes = "分页带条件查询产品列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功", response = ResponseObj.class),
            @ApiResponse(code = 500, message = "服务器内部异常", response = String.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productType", value = "产品编号"),
            @ApiImplicitParam(name = "pageNo", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页数量"),
    })
    @GetMapping("v1/product/page")
    public ResponseObj queryByPage(@RequestParam(value = "productType") Integer productType,
                                   @RequestParam(value = "pageNo", required = false) Integer pageNo,
                                   @RequestParam(value = "pageSize", required = false) Integer pageSize) {

        //查询总记录数
        int totalRecord = productService.computerCountByType(productType);

        //如果记录数为空就返回fail
        if (totalRecord == 0) {
            return ResponseObj.fail();
        }

        pageNo = CommonUtils.getDefPageNo(pageNo);
        pageSize = CommonUtils.getDefPageSize(pageSize);

        //分页查询数据
        List<Product> productList = productService.queryProductByTypePage(productType, pageNo, pageSize);

        //创建分页模型
        PageInfo pageInfo = new PageInfo(pageNo, pageSize, totalRecord);

        //创建HashMap存放数据
        HashMap<String, Object> data = new HashMap<>(2);

        data.put("pageInfo", pageInfo);
        data.put("productList", productList);

        return ResponseObj.ok().content(data);

    }

    @ApiOperation(value = "查询产品的详细信息")
    @GetMapping("v1/product/detail")
    public ResponseObj queryByProductId(@RequestHeader(value = "uid") Integer uid,
                                        @RequestParam("pid") Integer productId
    ) {
        //查询产品
        Product product = productService.queryProductById(productId);
        ResponseObj responseObj = ResponseObj.fail();
        if (product != null) {
            responseObj = ResponseObj.ok();
            HashMap<String, Object> result = new HashMap<>(3);
            //将产品放入map中
            result.put("product", product);

            //查询用户购买记录
            List<UserBidInfo> bidInfoList = bidInfoService.selectByProdId(productId, 1, 5);
            result.put("bidInfoList", bidInfoList);

            if (uid != null && uid > 0) {
                FinanceAccount financeAccount = financeAccountService.queryByUserId(uid);
                result.put("availableMoney", financeAccount.getAvailableMoney());
            } else {
                result.put("availableMoney", -1);
            }
            //todo:查询用户购买记录
            responseObj.setContent(result);
        }
        return responseObj;
    }

    @ApiOperation(value = "查询用户的投资记录")
    @GetMapping("/v1/product/bidinfo")
    public ResponseObj getUserProductBid(@RequestHeader("uid") Integer uid,
                                         @RequestParam("pageNo") Integer pageNo,
                                         @RequestParam("pageSize") Integer pageSize) {
        ResponseObj result = ResponseObj.ok();
        //校验uid
        if (uid != null && uid > 0) {
            pageNo = CommonUtils.getDefPageNo(pageNo);
            pageSize = CommonUtils.getDefPageSize(pageSize);
            List<ProductBidInfo> productBidInfoList = productService.queryProductBidInfoByUid(uid, pageNo, pageSize);

            List<ProductBidView> viewList = new ArrayList<>();
            //dto -> view
            productBidInfoList.forEach(productBidInfo -> viewList.add(new ProductBidView(productBidInfo)));
            return result.content(viewList);
        }
        return result;
    }

    @ApiOperation(value = "查询用户收益列表")
    @GetMapping("/v1/product/incomeinfo")
    public ResponseObj getUserProductIncome(@RequestHeader("uid") Integer uid,
                                            @RequestParam("pageNo") Integer pageNo,
                                            @RequestParam("pageSize") Integer pageSize){
        ResponseObj result = ResponseObj.ok();
        if (uid != null && uid > 0){
            pageNo = CommonUtils.getDefPageNo(pageNo);
            pageSize = CommonUtils.getDefPageSize(pageSize);
            List<ProductIncomeInfo> productIncomeList = productService.queryProductIncomeInfoByUid(uid, pageNo, pageSize);

            List<ProductIncomeView> viewList = new ArrayList<>();
            //转换 dto -> view
            productIncomeList.forEach(productIncomeInfo -> viewList.add(new ProductIncomeView(productIncomeInfo)));
            return result.content(viewList);
        }
        return result;
    }

}
